package tech.heptagram.staff.cas.controller.view;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import tech.heptagram.core.common.rest.ResponseResult;
import tech.heptagram.staff.cas.common.CurrentUserUtil;
import tech.heptagram.staff.cas.domain.info.MenuInfo;
import tech.heptagram.staff.cas.domain.info.MenuTreeVo;
import tech.heptagram.staff.cas.domain.po.Menu;
import tech.heptagram.staff.cas.service.staff.CasMenuService;


/**
 * 菜单页面API
 *
 * @author zilaiye
 * @date 2017/12/7
 */
@RestController
@RequestMapping("/api/view/sys/menu")
public class CasMenuApiController {

    @Resource
    private CasMenuService casMenuService;

    /**
     * 获取指定角色的菜单权限列表
     *
     * @param roleId
     * @return
     */
    @PostMapping("/roleId/{roleId}")
    public ResponseResult<List<Integer>> findRoleMenuId(@PathVariable Integer roleId) {
        List<Integer> menuIds = casMenuService.findIdsByRoleId(roleId);
        return new ResponseResult<>(menuIds);
    }


    @PostMapping("allchilds/{parentId}")
    public ResponseResult<MenuTreeVo> findAllSubmenu(@PathVariable Integer parentId) {
        MenuTreeVo menuTrees = casMenuService.findTree(parentId);
        return new ResponseResult<>(menuTrees);
    }

    /**
     * 获取子菜单列表
     *
     * @param parentId parentId
     * @return child menu list
     */
    @PostMapping("childs/{parentId}")
    public ResponseResult<List<Menu>> findSubmenu(@PathVariable Integer parentId) {
        List<Menu> lstChildMenus = casMenuService.findByParentId(parentId);
        return new ResponseResult<>(lstChildMenus);
    }

    /**
     * 保存
     *
     * @param menu
     * @return
     */
    @PostMapping("save")
    public ResponseResult<Integer> save(@RequestBody Menu menu) {
        menu.setCreatedBy(CurrentUserUtil.getCurrentUser().getTrueName());
        Integer id = casMenuService.save(menu);
        return new ResponseResult<>(id);
    }

    /**
     * 更新菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("update")
    public ResponseResult<Boolean> update(@RequestBody Menu menu) {
        menu.setModifiedBy(CurrentUserUtil.getCurrentUser().getTrueName());
        casMenuService.update(menu);
        return new ResponseResult<>(true);
    }

    @PostMapping("/delete/{menuId}")
    public ResponseResult<Boolean> delete(@PathVariable Integer menuId) {
        casMenuService.delete(menuId, CurrentUserUtil.getCurrentUser().getTrueName());
        return new ResponseResult<>(true);
    }

    @PostMapping("/tree")
    public ResponseResult<MenuInfo> findStaffMenus(HttpServletRequest request) {
        String staffCode = CurrentUserUtil.getCurrentUser().getCode();

        String app = request.getParameter("app");

        MenuInfo menuInfo = casMenuService.findMenusByStaffCode(staffCode, app);
        return new ResponseResult<>(menuInfo);
    }
}
